<?php $this->headScript()
->appendFile($this->js('jquery-ui/ui/jquery.ui.dialog.js'))
->appendFile($this->js('plugins/jquery.form.js'))
?>
<script type="text/javascript">
function checkFail()
{
    $.each($(".score"),function(){
        var s = $(this).text();
        if (s !='' && s < 60)
              $(this).addClass('failed');
     });
}

$(document).ready(function(){

    	$("#sn").change(function(){
    		 $("#stageForm").submit();
        });
    	$("#stage_id").change(function(){
            $("#stageForm").submit();
       });
       // 新增平時成績
        $("#appendBtn").click(function(){
            $.post('<?php echo $this->route('input')?>',{
                sn : $("#sn").val(),
                stage_id: $("#stage_id").val(),
                mode:'append'
                 },function(response){
            	$('#dialog').html(response);
            	$('#dialog').dialog('open');
             });
         });

        // 刪除平時成績
        $(".deleteIcon").click(function(){
            if (confirm('<?php echo $this->translate("Are you sure want to delete it?")?>')) {
                var testOrder = $(this).attr('id').substr(7);
                var sn = $("#sn").val();
                var stage_id = $("#stage_id").val();
                $.getJSON('<?php echo $this->route("delete")?>',
                		{sn:sn, stage_id:stage_id, test_order:testOrder},
                		function(data){
                    		if (data)
                    			  $("#stage_id").trigger('change');
                    	}
                );
            }
          });

        // 修改平時成績
        $(".editIcon").click(function(){
            var testOrder = $(this).attr('id').substr(5);
            $.post('<?php echo $this->route("input")?>', {
                sn : $("#sn").val(),
                stage_id: $("#stage_id").val(),
                test_order : testOrder
                 },function(response){
                $('#dialog').html(response);
                $('#dialog').dialog('open');
             });
          });

        // 匯出平時成績到階段成績中
        $("#toStageBtn").click(function(){
            if (confirm('<?php echo $this->translate("The old results will be overwritten, Determined to do?")?>')) {
                $(this).attr('disabled','disabled');
              $("#stageForm").attr('action','<?php echo $this->route("export")?>').submit();
            }
          });

        //  輸入 dialog
        $("#dialog").dialog({
            bgiframe: true,
            autoOpen: false,
            width:480,
            height: 450,
            modal: true,
            focus: function(){
        	 $("#inputForm .score:first").focus();
            }
        });

        // 輸入處理
         $("#inputForm .score").live('keydown',function(e){

        var tabIndex =  parseInt($(this).attr('tabindex'));
        switch (e.which) {
        case 9 : //tab
            var score = parseFloat($(this).attr('value'));
            if (isNaN(score)) score = -1;
            if (score < 0 || score > 100) {
                $(this).attr('value','');
                tabIndex --;
            }
            break;
        case 13 : // return
            var score = parseFloat($(this).attr('value'));
            if (isNaN(score)) score = -1;
            if (score < 0 || score > 100) {
                alert($(this).attr('value')+' <?php echo $this->translate('value error')?>');
                $(this).attr('value','');
            }
            else
                tabIndex++;
            break;
        case 40 :
            tabIndex ++;
         break;
        case 38 :
            tabIndex --;
         break;
        }
        if ($("#inputForm .score[tabindex="+tabIndex+"]").length) {
            $("#inputForm .score[tabindex="+tabIndex+"]").focus();
        }
//        else
//            $("#signBtn").focus();
    });
    $("#stage-list table tbody tr:even").addClass('even');
    $("#stage-list table tbody tr:odd").addClass('odd');
    checkFail();
});
</script>
<style>
.failed {color: red}
.score {text-align:right}
.weighted{color: blue}
.inputTitle{color:blue}
.number , .studentName {white-space:nowrap}
.sent {margin:10px; padding:3px; background: #ff5; border: #ccc solid thin}
#stage-list table{margin:5px; padding:2px;border:#aaa solid thin;background:#ccc}
#stage-list table th {background:#cdd}
#stage-list table td {padding:2px 5px}
</style>
<div id="normal-data">
<form id="stageForm"  method="post" action="<?php echo $this->route('index')?>">
<input type="hidden" id="editMode" name="editMode" value="0" />
<div id="stage-menu">
<select id="sn" name="sn">
<?php foreach($this->teacherCourse as $val):?>
<option value="<?php echo $val['sn']?>" <?php if ($this->sn == $val['sn']):?>selected<?php endif?>><?php echo $val['className']?> - <?php echo $val['name']?></option>
<?php endforeach?>
</select>
<select id="stage_id" name="stage_id">
<?php if($this->currStage == 255):?>
<option value="<?php echo $this->currStage?>"><?php echo $this->translate('whole stage score')?></option>
<?php else:?>
<?php for($i=1; $i <= $this->period; $i++):?>
<option value="<?php echo $i?>" <?php if($i==$this->currStage):?>selected<?php endif?> ><?php echo sprintf($this->translate('%dth stage'),$i)?></option>
<?php endfor;?>
<?php endif?>
</select>
<?php if (!$this->sendmit):?>
<input type="button" id="appendBtn" value="<?php echo $this->translate('Add usually score')?>" />
<?php if ($this->scores):?>
<input type="button" id="toStageBtn" value="<?php echo $this->translate('Export to stage score')?>" />
<?php endif?>
<?php else:?>
<span class="sent"><?php echo $this->translate('Score have been sent the Registry staff')?></span>
<?php endif?>
</div>

<div id="stage-list">
<table>
<thead>
<tr>
<th><?php echo $this->translate('number')?></th>
<th><?php echo $this->translate('student name')?></th>
<?php foreach($this->scores as $testOrder=>$val):?>
<th><?php echo $val['test_name']?><br/>
<span class="weighted"><?php echo $this->translate('weighted')?>: <?php echo $val['weighted']?></span>
<?php if (!$this->sendmit):?>
<br />
<a href="#"  class="editIcon" id="edit-<?php echo $testOrder?>"><img src="<?php echo $this->baseUrl?>/images/wedit.png" border="0"></a>
<a href="#"  class="deleteIcon" id="delete-<?php echo $testOrder?>"><img src="<?php echo $this->baseUrl?>/images/del.png" border="0"></a>
<?php endif?>
</th>
<?php endforeach?>
<?php if ($this->scores):?>
<th><?php echo $this->translate('Average')?></th>
<?php endif?>
</tr>
</thead>
<tbody>
<?php $testOrderArr = array_keys($this->scores)?>
<?php $i=0;?>
<?php foreach($this->students as $studentSn=>$val):?>
<tr>
<td class="number"><?php echo $val['number']?></td>
<td class="studentName"><?php echo $val['name']?></td>
<?php foreach($testOrderArr as $testOrder):?>
<td class="score"><?php if (isset($this->scores[$testOrder]['score'][$studentSn])) echo $this->scores[$testOrder]['score'][$studentSn]?></td>
<?php endforeach?>
<?php if ($this->scores):?>
<td class="score"><?php if (isset($this->scoreAvg[$val['sn']]))echo $this->scoreAvg[$val['sn']]?></td>
<?php endif?>
</tr>
<?php endforeach;?>

</tbody>
</table>
</div>

</form>
</div>

<div id="dialog" title=" <?php echo $this->translate('Input usually score')?>">

</div>
